<?php 
// +----------------------------------------------------------------------
// | RXThinkCMF敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2023 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 RXThinkCMF并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: https://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: @牧羊人 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\School;
use app\admin\model\Departments;
use app\admin\model\Grade;
use app\admin\model\Professional;
use app\admin\model\Member;


use app\admin\service\ClssService;

/**
 * 班级管理-控制器
 * @author 牧羊人
 * @since: 2023/07/31
 * Class Clss
 * @package app\admin\controller
 */
class Clss extends Backend
{
    /**
     * 初始化方法
     * @author 牧羊人
     * @since: 2023/07/31
     */
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->model = new \app\admin\model\Clss();
        $this->service = new ClssService();
    }

    /**
     * 获取数据列表
     * @return mixed
     * @since 2020/11/11
     * @author 牧羊人
     */
    public function index()
    {
        $result = $this->service->getList();
        return $this->jsonReturn($result);
    }

    /**
     * 获取班级列表
     * @return mixed
     * @since 2020/11/20
     * @author 牧羊人
     */
    public function getClassList()
    {
        $result = $this->service->getClassList();
        return $this->jsonReturn($result);
    }

    /**
     * 获取班级列表
     * @return mixed
     * @since 2020/11/20
     * @author 牧羊人
     */
    public function getClazzList()
    {
        $result = $this->service->getList();
        return $this->jsonReturn($result);
    }

    /**
     * 获取名单分页列表
     * @return mixed
     * @since 2020/11/20
     * @author 牧羊人
     */
    public function getStuList()
    {
        $result = $this->service->getStuList();
        return $this->jsonReturn($result);
    }

    /**
     * 获取名单列表
     * @return mixed
     * @since 2020/11/20
     * @author 牧羊人
     */
    public function getStudentsList()
    {
        $result = $this->service->getStudentsList();
        return $this->jsonReturn($result);
    }

    /**
     * 保存班级名单
     * @return mixed
     * @since 2020/11/20
     * @author 牧羊人
     */
    public function saveImportlist()
    {
        $result = $this->service->saveImportlist();
        return $this->jsonReturn($result);
    }

    /**
     * 保存导入班级名单
     * @return mixed
     * @since 2020/11/20
     * @author 牧羊人
     */
    public function saveImportroll()
    {
        $result = $this->service->saveImportroll();
        return $this->jsonReturn($result);
    }

    /**
     * 设置手环状态
     * @return mixed
     * @since 2020/11/19
     * @author 牧羊人
     */
    public function braceletstatus()
    {
        // 参数
        $param = request()->param();
        // 记录ID
        $ids = getter($param, "id");
        if (empty($ids)) {
            return $this->jsonReturn(message("记录ID不能为空", false));
        }
        if (is_array($ids)) {
            // 批量操作
            $result = $this->model->setDAll($ids,$param['braceletstatus']);
            if (!$result) {
                return $this->jsonReturn(message("设置失败", false));
            }
            return $this->jsonReturn(message("设置成功"));
        }else{
            $error = '';
            $item = [
                'id' => $param['id'],
                'braceletstatus' => $param['braceletstatus']
            ];
            if ($param['braceletstatus']==1) {
                $item['bracelet_start'] = time();
                $item['bracelet_end'] = 0;
            }
            if ($param['braceletstatus']==0) {
                $item['bracelet_end'] = time();
            }
            $rowId = $this->model->edit($item, $error);
            if (!$rowId) {
                return $this->jsonReturn(message($error, false));
            }
            return $this->jsonReturn(message());
        }
        
    }

    /**
     * 导入Excel
     * @author 牧羊人
     * @since 2021/5/24
     */
    public function importExcel()
    {
        $error = "";
        // 上传文件(非图片)
        $result = upload_file('file', '', $error);
        if (!$result) {
            return $this->jsonReturn(message($error, false));
        }
        // 文件路径
        $filePath = UPLOAD_TEMP_PATH . $result;
        if (!file_exists($filePath)) {
            return $this->jsonReturn(message("文件不存在", false));
        }
        // 读取文件
        $objPHPExcel = \PHPExcel_IOFactory::load($filePath); //获取sheet表格数目
        $sheetCount = $objPHPExcel->getSheetCount(); //默认选中sheet0表
        $sheetSelected = 0;
        $objPHPExcel->setActiveSheetIndex($sheetSelected); //获取表格行数
        $rowCount = $objPHPExcel->getActiveSheet()->getHighestRow(); //获取表格列数
        $columnCount = $objPHPExcel->getActiveSheet()->getHighestColumn();
        // 计数器
        $totalNum = 0;
        // 循环读取行数据
        for ($row = 2; $row <= $rowCount; $row++) {
            // 获取列值
            $dataArr = array();
            // 编号
            $dataArr['code'] = $objPHPExcel->getActiveSheet()->getCell("A" . $row)->getValue();
            // 班级名称
            $dataArr['name'] = $objPHPExcel->getActiveSheet()->getCell("B" . $row)->getValue();
            // 所属组织
            $organ = $objPHPExcel->getActiveSheet()->getCell("C" . $row)->getValue();
            $school = new School();
            $dataArr['organization'] = $school->where(['name'=>trim($organ),'mark'=>1])->value('id');
            // 年级
            $grade = new Grade();
            $gname = $objPHPExcel->getActiveSheet()->getCell("D" . $row)->getValue();
            $dataArr['grade'] = $grade->where(['organization'=>$dataArr['organization'],'name'=>trim($gname),'mark'=>1])->value('id');

            $dataArr['stype'] = $objPHPExcel->getActiveSheet()->getCell("E" . $row)->getValue()=='自然院系'?1:2;
            // 所属院系
            $superior = $objPHPExcel->getActiveSheet()->getCell("F" . $row)->getValue();
            $departments = new Departments();
            $dataArr['superior'] = $departments->where(['name'=>trim($superior),'organization'=>$dataArr['organization'],'mark'=>1])->value('id');
            //专业
            $professional = new Professional();
            $pname = $objPHPExcel->getActiveSheet()->getCell("G" . $row)->getValue();
            $dataArr['professional'] = $professional->where(['superior'=>$dataArr['superior'],'name'=>trim($pname),'mark'=>1])->value('id');
            // 状态
            $status = $objPHPExcel->getActiveSheet()->getCell("H" . $row)->getValue();
            $dataArr['status'] = $status == "正常" ? 1 : 2;
            // 班级老师
            $dataArr['header'] = $objPHPExcel->getActiveSheet()->getCell("I" . $row)->getValue();
            // 排序
            $dataArr['sort'] = $objPHPExcel->getActiveSheet()->getCell("J" . $row)->getValue();
            // 备注
            $dataArr['note'] = $objPHPExcel->getActiveSheet()->getCell("K" . $row)->getValue();
            $ids = $this->model->where(['name'=>$dataArr['name'],'organization'=>$dataArr['organization'],'stype'=>$dataArr['stype'],'superior'=>$dataArr['superior']])->value('id');
            if ($ids) {
               $dataArr['id'] = $ids;
            }
            // 插入数据
            $result = $this->model->edit($dataArr);
            if ($result) {
                $totalNum++;
                // $thirdid = 0;
                // if (isset($data['id'])) {
                //     $thirdid = \app\admin\model\Clss::where('id',$data['id'])->value('thirdclass');
                // }
                // if (!$thirdid) {
                //     $where = [
                //         ['grade','=',$data['grade']]
                //     ];
                //     $clist = \app\admin\model\Clss::where($where)->order('id ASC')->column('id');
                //     if ($clist) {
                //         $cid = array_search($rowId,$clist)+1;
                //     }else{
                //         $cid = 1;
                //     }
                    
                    
                //     $incode = \app\admin\model\School::where(['id'=>$data['organization']])->value('code');
                //     $gradeid = \app\admin\model\Grade::where(['id'=>$data['grade']])->value('thirdgrade');
                //     $datas = [
                //         "inviteCode"=>$incode,
                //         "schoolInfo"=>[
                //             ["grade"=>$gradeid,"classList"=>[$cid]]
                //         ]
                //     ];
                //     $results = $this->service->addClazz($datas);//同步第三方数据
                //     if ($results['code']==200) {
                //         \app\admin\model\Clss::where(['id'=>$rowId])->update(['thirdclass'=>$cid]);
                //     }else{
                //         //halt($results);
                //     }
            }
        }
        return $this->jsonReturn(message("本次共导入{$totalNum}条数据", true));
    }

    /**
     * 导入名单Excel
     * @author 牧羊人
     * @since 2021/5/24
     */
    public function batchExcel()
    {
        // 参数
        $param = request()->param();
        $member = new Member();
        $error = "";
        // 上传文件(非图片)
        $result = upload_file('file', '', $error);
        if (!$result) {
            return $this->jsonReturn(message($error, false));
        }
        // 文件路径
        $filePath = UPLOAD_TEMP_PATH . $result;
        if (!file_exists($filePath)) {
            return $this->jsonReturn(message("文件不存在", false));
        }
        // 导入名单前将之前名单数据解除班级绑定
        $fwhere[] = ['features', 'like', '%'.strval($param['class']).'%'];
        $features = $member->where($fwhere)->column('id');
        if ($features) {
            foreach ($features as $vv) {
                // 学员学籍号
                $mmf = $member->where('id',$vv)->find();
                $mfeature = json_decode($mmf['features'],true);
                if (!is_array($mfeature)) {
                    $mfeature = [];
                }else{
                    if ($mfeature[0]=='undefined') {
                        $mfeature = [];
                    }
                }
                if (in_array(strval($param['class']),$mfeature)) {
                    $ffeature = array_diff($mfeature,array(strval($param['class'])));
                    $member->update(['features'=>json_encode(array_values($ffeature))],['id'=>$vv]);
                }
            }
        }
        // 读取文件
        $objPHPExcel = \PHPExcel_IOFactory::load($filePath); //获取sheet表格数目
        $sheetCount = $objPHPExcel->getSheetCount(); //默认选中sheet0表
        $sheetSelected = 0;
        $objPHPExcel->setActiveSheetIndex($sheetSelected); //获取表格行数
        $rowCount = $objPHPExcel->getActiveSheet()->getHighestRow(); //获取表格列数
        $columnCount = $objPHPExcel->getActiveSheet()->getHighestColumn();
        // 计数器
        // $totalNum = 0;
        $dnum = 0;//导入几条
        $wnum = 0;//导入失败几条
        
        $carr = [];
        // 循环读取行数据
        for ($row = 2; $row <= $rowCount; $row++) {

            // 学员学籍号
            $roll = $objPHPExcel->getActiveSheet()->getCell("B" . $row)->getValue();
            $mm = $member->where('roll',$roll)->where('mark',1)->find();
            // var_dump($roll);echo "<pre>";
            if ($mm) {
                $features = json_decode($mm['features'],true);
                if (!is_array($features)) {
                    $features = [];
                }else{
                    if (isset($features[0])&&$features[0]=='undefined') {
                        $features = [];
                    }
                }
                if (!in_array(strval($param['class']),$features)) {
                    array_push($features,strval($param['class']));
                    $member->update(['features'=>json_encode($features)],['id'=>$mm['id']]);
                }
                array_push($carr,strval($mm['id']));
                $dnum++;
            }else{
                $wnum++;
            }
        }
        // halt(json_encode($carr));
        $this->model->update(['features'=>json_encode($carr)],['id'=>$param['class']]);
        return $this->jsonReturn(message($dnum."条导入成功，".$wnum."条导入失败"));
    }

    /**
     * 导入手环Excel
     * @author 牧羊人
     * @since 2021/5/24
     */
    public function braceletImportExcel()
    {
        $error = "";
        // 上传文件(非图片)
        $result = upload_file('file', '', $error);
        if (!$result) {
            return $this->jsonReturn(message($error, false));
        }
        // 文件路径
        $filePath = UPLOAD_TEMP_PATH . $result;
        if (!file_exists($filePath)) {
            return $this->jsonReturn(message("文件不存在", false));
        }
        // 读取文件
        $objPHPExcel = \PHPExcel_IOFactory::load($filePath); //获取sheet表格数目
        $sheetCount = $objPHPExcel->getSheetCount(); //默认选中sheet0表
        $sheetSelected = 0;
        $objPHPExcel->setActiveSheetIndex($sheetSelected); //获取表格行数
        $rowCount = $objPHPExcel->getActiveSheet()->getHighestRow(); //获取表格列数
        $columnCount = $objPHPExcel->getActiveSheet()->getHighestColumn();
        // 计数器
        $totalNum = 0;
        $member = new Member();
        // 循环读取行数据
        for ($row = 2; $row <= $rowCount; $row++) {
            // 获取列值
            $dataArr = array();
            // 学员学籍号
            $roll = $objPHPExcel->getActiveSheet()->getCell("B" . $row)->getValue();
            // 手环标签号
            $code = $objPHPExcel->getActiveSheet()->getCell("C" . $row)->getValue();
            // 清空之前导入的手环
            //$result = $member->update(['bracelet'=>null],['bracelet'=>$code]);
            // 更新数据
            $result = $member->update(['bracelet'=>$code],['roll'=>$roll]);
            if ($result) {
                $totalNum++;
            }
        }
        return $this->jsonReturn(message("本次共更新{$totalNum}条数据", true));
    }

    /**
     * 导出Excel
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     * @since 2021/3/25
     * @author 牧羊人
     */
    public function exportExcel()
    {
        // 参数
        $param = request()->param();
        // 查询条件
        $map = [];

         // 所属组织
        $organization = isset($param['organization']) ? (int)$param['organization'] : '';
        if ($organization) {
            $map[] = ['organization', '=', $organization];
        }

        // 所属年级
        $grade = isset($param['grade']) ? (int)$param['grade'] : '';
        if ($grade) {
            $map[] = ['grade', '=', $grade];
        }

        // 所属院系
        $superior = isset($param['superior']) ? (int)$param['superior'] : '';
        if ($superior) {
            $map[] = ['superior', '=', $superior];
        }

        // 所属专业
        $professional = isset($param['professional']) ? (int)$param['professional'] : '';
        if ($professional) {
            $map[] = ['professional', '=', $professional];
        }

        // 班级名称
        $name = isset($param['name']) ? trim($param['name']) : '';
        if ($name) {
            $map[] = ['name', 'like', "%{$name}%"];
        }

        // 类型
        $stype = isset($param['stype']) ? (int)$param['stype'] : 0;
        if ($stype) {
            $map[] = ['stype', '=', $stype];
        }

        // 状态
        $status = isset($param['status']) ? (int)$param['status'] : 0;
        if ($status) {
            $map[] = ['status', '=', $status];
        }

        // 更新时间
        $starttime = isset($param['starttime']) ? trim($param['starttime']) : '';
        $endtime = isset($param['endtime']) ? trim($param['endtime']) : '';
        if ($starttime&&$endtime) {
            $map[] = ['update_time', '>= time', $starttime];
            $map[] = ['update_time', '<= time', $endtime];
        }
        $map[] = ['mark', "=", 1];
        // 获取导出的数据源
        $list = $this->model->where($map)->select()->toArray();

        // 实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        // 激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '班级编号')
            ->setCellValue('B1', '班级名称')
            ->setCellValue('C1', '所属学校')
            ->setCellValue('D1', '年级')
            ->setCellValue('E1', '院系类型')
            ->setCellValue('F1', '所属院系')
            ->setCellValue('G1', '专业')
            ->setCellValue('H1', '状态')
            ->setCellValue('I1', '班级老师')
            ->setCellValue('J1', '显示顺序')
            ->setCellValue('K1', '备注');
        // 设置表格头水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('H')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('I')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('J')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('K')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        // 设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('H')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('I')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('J')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('K')->setWidth(10);
        $school = new School();
        $departments = new Departments();
        $grade = new Grade();
        $professional = new Professional();
        // 循环刚取出来的数组，将数据逐一添加到excel表格。
        for ($i = 0; $i < count($list); $i++) {
            
            $organization = $school->where('id',$list[$i]['organization'])->value('name');
            $superior = $departments->where('id',$list[$i]['superior'])->value('name');
            $grade_name = $grade->where('id',$list[$i]['grade'])->value('name');
            $professional_name = $grade->where('id',$list[$i]['professional'])->value('name');

            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['code']);// 班级编号
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['name']);// 班级名称
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $organization);// 所属学校
            $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 2), $grade_name);// 所属年级
            $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 2), $list[$i]['stype']==1?'自然院系':'体育院系');// 院系类型
            $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 2), $superior);// 所属院系
            $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 2), $professional_name);// 所属专业
            $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 2), $list[$i]['status']==1 ? "正常" : "停用");// 状态
            $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 2), $list[$i]['header']);// 班级老师
            $objPHPExcel->getActiveSheet()->setCellValue('J' . ($i + 2), $list[$i]['sort']);// 排序
            $objPHPExcel->getActiveSheet()->setCellValue('K' . ($i + 2), $list[$i]['note']);// 备注
        }
        // 设置保存的Excel表格名称
        $filename = '班级管理_' . date('YmdHis', time()) . ".xlsx";
        // 设置当前激活的sheet表格名称
        $objPHPExcel->getActiveSheet()->setTitle('班级管理');

        // 保存本地文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save(UPLOAD_TEMP_PATH . "/" . $filename);
        return download(UPLOAD_TEMP_PATH . "/" . $filename, $filename);
        // 文件地址
//        $filePath = get_image_url('/uploads'.str_replace(ATTACHMENT_PATH, "", UPLOAD_TEMP_PATH . "/" . $filename));
//        return $this->jsonReturn(message("操作成功", true, $filePath));
    }

    /**
     * 导出手环记录Excel
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     * @since 2021/3/25
     * @author 牧羊人
     */
    public function exportbraceletExcel()
    {
        // 参数
        $param = request()->param();
        // 查询条件
        $where = [];
        if ($param['class']) {
            $str = '"'.$param['class'].'"';
            $where[] = ['features', 'like','%'.$str.'%'];
        }
        $where[] = ['mark', "=", 1];
        // 获取导出的数据源
        $member = new Member();
        $list = $member->where($where)->select()->toArray();

        // 实例化PHPExcel类
        $objPHPExcel = new \PHPExcel();
        // 激活当前的sheet表
        $objPHPExcel->setActiveSheetIndex(0);
        //5.设置表格头（即excel表格的第一行）
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '学院姓名')
            ->setCellValue('B1', '学员学籍号')
            ->setCellValue('C1', '手环标签号');
        // 设置表格头水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C1')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置列水平居中
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        // 设置单元格宽度
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(10);
        $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(10);
        $school = new School();
        $departments = new Departments();
        $grade = new Grade();
        $professional = new Professional();
        // 循环刚取出来的数组，将数据逐一添加到excel表格。
        for ($i = 0; $i < count($list); $i++) {

            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i + 2), $list[$i]['realname']);// 学员姓名
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 2), $list[$i]['roll']);// 学员学籍号
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 2), $list[$i]['bracelet']);// 手环标签号
        }
        // 设置保存的Excel表格名称
        $filename = '手环名单_' . date('YmdHis', time()) . ".xlsx";
        // 设置当前激活的sheet表格名称
        $objPHPExcel->getActiveSheet()->setTitle('手环名单');

        // 保存本地文件
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save(UPLOAD_TEMP_PATH . "/" . $filename);

        // 文件地址
        $filePath = get_image_url('/uploads'.str_replace(ATTACHMENT_PATH, "", UPLOAD_TEMP_PATH . "/" . $filename));
        return $this->jsonReturn(message("操作成功", true, $filePath));
    }
	
                            	                                
}